package com.sogou.ime.ui.filemgr;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.util.EncodingUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class LanguageXML {
//	private static final String ROOT = "Language";

    public static final String ACTIVE_LANGUAGE = "activeLanguage";

    public static final String ENABLE_LANGUAGE = "enableLanguage";
    
    public static final String DISABLE_LANGUAGE = "disableLanguage";

    private String mPath = null;
    
    private String active_language = null;
    
    private List<String> enable_language = new ArrayList<String>();
    
    private List<String> disable_language = new ArrayList<String>();
    
//    String test1(String str1, String tag1, String tag2);
//    String test2(String path);  
//    String string  = test1(c), tag1, tag2);
        
    public LanguageXML(String path) {
        mPath = path;
        parse();
    } 

	public String getActive_language() {
		return active_language;
	}

	public void setActive_language(String active_language) {
		this.active_language = active_language;
	}

	public List<String> getEnable_language() {
		return enable_language;
	}

	public void setEnable_language(List<String> enable_language) {
		this.enable_language = enable_language;
	}

	public List<String> getDisable_language() {
		return disable_language;
	}

	public void setDisable_language(List<String> disable_language) {
		this.disable_language = disable_language;
	}

	public String getmPath() {
		return mPath;
	}

	public void setmPath(String mPath) {
		this.mPath = mPath;
	}
	
	public static String readFile(String filePath) {
		String text = null;
		if (filePath != null) {
			try {
				FileInputStream fin = new FileInputStream(filePath);
				int length = fin.available();
				byte[] buffer = new byte[length];
				fin.read(buffer);
				text = EncodingUtils.getString(buffer, "UTF-8");
				fin.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return text;
	}
    
    public void parse() {
        if (mPath == null) {
            return;
        }

        String xmlBuf = readFile(mPath);

        if (xmlBuf == null) {
            return;
        }

        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new ByteArrayInputStream(xmlBuf
                    .replaceAll("&", "&amp;").getBytes()));
            NodeList nodeList = doc.getDocumentElement().getChildNodes();            
            
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);

                if (node.getNodeName().equals(ACTIVE_LANGUAGE)) {          
                    active_language = node.getTextContent();
                }else if (node.getNodeName().equals(ENABLE_LANGUAGE)) {
					enable_language.add(node.getTextContent());
				}else if (node.getNodeName().equals(DISABLE_LANGUAGE)) {
					disable_language.add(node.getTextContent());
				}
            }
        } catch (Exception e) {
            return;
        }
    }
}
